/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.hibernate.model.dao;

import hibernate.pojo.Admin;
import hibernate.util.HibernateUtil;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Administrator
 */
public class AdminAdminDAO 
{
    //<editor-fold defaultstate="collapsed" desc="3. Đăng nhập khách hàng">
     public static Admin DangNhapAdmin(String taiKhoan, String matKhau)
     {
         Admin admin = null;
         Session session = HibernateUtil.getSessionFactory().openSession();
         try
         {
             String hql = "select am from Admin am where am.taiKhoan=:taiKhoan and am.matKhau=:matKhau";
             Query query = session.createQuery(hql);
             query.setString("taiKhoan", taiKhoan);
             query.setString("matKhau", matKhau);
             Object ob = query.uniqueResult();
             admin = (Admin)ob;
         }
         catch(HibernateException hex)
         {
             System.err.print(hex);
         }
         finally
         {
             session.close();
         }
         return admin;
     }
     //</editor-fold>
     
    //<editor-fold defaultstate="collapsed" desc="6. Encrypt pass">
    public static String encryptPassword(String text){
        String encode = "";
        try
        {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(text.getBytes());
            BigInteger dis = new BigInteger(1, md5.digest());
            encode = dis.toString(16);
        }
        catch(NoSuchAlgorithmException e)
        {
            e.toString();
        }
        return encode;
    }
    //</editor-fold>
}
